package likouJava;

import java.util.HashMap;

public class 无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> charIndexMap = new HashMap<>();
        int left = 0;
        int maxLength = 0;
        for(int right = 0;right<s.length();right++){
            char c = s.charAt(right);
            if(charIndexMap.containsKey(c) && charIndexMap.get(c) >= left){
                left = charIndexMap.get(c)+1;
            }
            charIndexMap.put(c,right);
            int length = right - left +1;
            maxLength = Math.max(maxLength,length);
        }
        return maxLength;
    }
}
